import config from "./config.js";
export const requet=(params)=>{
	return new Promise((resolve,reject)=>{
		let host=params.host||config.apiHost;
		let method=params.method||"POST";
		let header=params.header||{
				"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjE0MiwiaWF0IjoxNjE3NjgwMTMxLCJleHAiOjE2MjI4NjQxMzF9.u-lLH7nKY-MR63WR8-WfXUaEpG4fY6uGygsIjT9DJcQ",
			};
		let data=params.data||{};
		let url=params.url||'';
		uni.request({
			url: host+url,
			method,
			header,
			data,
			success: (result) => {resolve(result.data);},
			fail: (err) => {reject(err);},
			complete: () => {
			}
		})
	});
}
//读取本地缓存
export const getCache=(key)=>{
	return new Promise((resolve,reject)=>{
		uni.getStorage({
			key,
			success(rs){
				resolve(rs.data);
			},
			fail(err){
				reject(err);
			}
		})
	});
}
//保存到本地缓存
export const setCache=(key,data)=>{
	return new Promise((resolve,reject)=>{
		uni.setStorage({
			data,
			key,
			success(){
				resolve(true);
			},
			fail(err){
				reject(err);
			}
		})
	});
}
//选择图片
export const chooseImg=(count)=>{
	return new Promise((resolve,reject)=>{
		uni.chooseImage({
			count,
			success(data){
				resolve(data);
			},
			fail(err){
				reject(err);
			}
		})
	});
}

/**
 * 上传文件
 * @param url 主机地址
 * @param filepath 本地选取的图片临时地址
 * @param name 后台获取文件的参数名
 * @param formData 传递的参数，对象
 */
export const uploadFile = (url,filePath,name,formData)=>{
	return new Promise((resolve,reject)=>{
		uni.uploadFile({
			url,
			filePath,
			name,
			formData,
			success(data) {
				resolve(data);
			},
			fail(err) {
				reject(err);
			}
		});
	});
}